package com.ms.qw.infraestructura.persistence.jpa.sigar;

import com.ms.qw.client.dto.*;
import com.ms.qw.domain.model.Campana;
import com.ms.qw.domain.model.CampanaRepository;
import com.ms.qw.domain.model.Receta;
import com.ms.qw.domain.model.RecetaRepository;
import com.ms.qw.server.infrastructure.mapper.MSResultTransformer;
import com.ms.qw.server.infrastructure.persistence.util.InClauseConstructor;
import com.ms.qw.server.infrastructure.persistence.util.ValueProvider;
import com.ms.support.jpa.BaseDAO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Repository
public class CampanaRepositoryImpl extends BaseDAO<Campana, Integer> implements CampanaRepository {

    private final static Logger logger = LoggerFactory.getLogger(CampanaRepositoryImpl.class);


    public CampanaDTO findById(Integer value) {

        StringBuffer sql = new StringBuffer();
        sql.append("        SELECT\n" +
                "            GMC.[iCodCampana] as \"codigo\",\n" +
                "            GMC.[vCampana] as \"nombre\",\n" +
                "            GMC.[dFecInicio] as \"fechaInicio\",\n" +
                "            GMC.[dFecFin] as \"fechaFin\",\n" +
                "            GMC.[cTipoCampana] as \"tipoCampana\",\n" +
                "            GMC.[cCategoriaCampana] as \"categoriaCampana\",\n" +
                "            GMC.[iCodCampanaRef] as \"codigoCampanaReferencia\",\n" +
                "            GMC_REF.[vCampana] as \"nombreCampanaReferencia\" ,\n" +
                "            GMC.[cEstado] as \"estado\" \n" +
                "        FROM\n" +
                "            [EQGENERAL].[GEMAECAMPANA] GMC\n" +
                "            LEFT JOIN [EQGENERAL].[GEMAECAMPANA] GMC_REF\n" +
                "             ON GMC_REF.iCodCampana = GMC.iCodCampanaRef\n" +
                "        WHERE\n" +
                "            GMC.iCodCampana = "+value);

        org.hibernate.Query query = em().unwrap(Session.class)
                .createSQLQuery(sql.toString())
                .setResultTransformer(new MSResultTransformer(CampanaDTO.class));
        CampanaDTO result= (CampanaDTO) query.uniqueResult();
        return result;
    }


}
